package stagefour.servlet;


import com.alibaba.fastjson2.JSONObject;
import stagefour.model.ResultModel;
import stagefour.model.UserModel;
import stagefour.service.UserService;
import stagefour.service.impl.UserServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.logging.Logger;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    static Logger logger = Logger.getLogger("login");

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        logger.info("123123123123123");
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        UserModel userModel = new UserModel();
        UserService userService = new UserServiceImpl();

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        userModel.setUsername(username);
        userModel.setPassword(password);


        ResultModel result = userService.login(userModel);
        if (result.getMsg().equals("success")) {
            req.getSession().setAttribute("username", username);

        }
        resp.getWriter().println(JSONObject.toJSONString(result));




    }
}
